package game.java3D;

import java.applet.Applet;
import java.awt.*;
import java.net.MalformedURLException;
import java.net.URL;

import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.image.TextureLoader;

import com.sun.j3d.utils.universe.SimpleUniverse;

import javax.media.j3d.*;
import javax.vecmath.*;


public class WegaApplet extends Applet
{
	private static final long serialVersionUID = 1L;
	private SimpleUniverse universe = null;
	
	public BranchGroup createSceneGraph( SimpleUniverse u )
	{
		Color3f eColor = new Color3f(0.0f, 0.0f, 0.0f);
	    Color3f sColor = new Color3f(1.0f, 1.0f, 1.0f);
	    Color3f zlta = new Color3f( Color.yellow );
	    Point3d pointD = new Point3d( 0.0, 0.0, 0.0 );
	    Point3f pointF = new Point3f( 0.0f, 0.0f, 0.0f );
		
		BranchGroup branch = new BranchGroup();
		
		TransformGroup objScale = new TransformGroup();
		objScale.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
		objScale.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
		Transform3D t3d = new Transform3D();
		t3d.setScale( 1 );
		objScale.setTransform( t3d );
		branch.addChild( objScale );
		
		BoundingSphere bounds = new BoundingSphere( pointD, 100.0 );
		
		URL url;
		TextureLoader loader;
		Background bg = new Background();
		bg.setColor( new Color3f( 0.1f, 0.1f, 0.1f ) );
		try 
		{
			url = new URL( "http://andrej-cicmansky.own.cz/Obrazky/universe.jpg" );
			loader = new TextureLoader( url , this );
			ImageComponent2D image = loader.getImage(); 
			bg.setImage( image );
		} 
		catch ( MalformedURLException e ) 
		{
			e.printStackTrace();
		}		
		bg.setApplicationBounds( bounds );
		objScale.addChild( bg );
		
		Texture textura;
		int primflags = Primitive.GENERATE_NORMALS + Primitive.GENERATE_TEXTURE_COORDS;
		TextureAttributes texA = new TextureAttributes();
		texA.setTextureMode( TextureAttributes.REPLACE );
		TransparencyAttributes trasparent = new TransparencyAttributes( TransparencyAttributes.NICEST, 0.1f );
		Material mat = new Material( zlta, eColor, zlta, sColor, 128 );
		mat.setLightingEnable( true );		
		Appearance ap = new Appearance();
		ap.setTransparencyAttributes( trasparent );
		ap.setMaterial( mat );
		try 
		{
			url = new URL( "http://andrej-cicmansky.own.cz/Obrazky/textury/slnko.jpg" );
			loader = new TextureLoader( url , this );
			textura = loader.getTexture();				
			ap.setTexture( textura );
			ap.setTextureAttributes( texA );
		} 
		catch ( MalformedURLException e ) 
		{
			e.printStackTrace();
		}
		Sphere slnko = new Sphere( 0.15f, primflags, ap );
		slnko.setPickable( true );
		objScale.addChild( slnko );
				
		//vytvorenie planet a ich pozicii
		TransformGroup posunG = new TransformGroup();
		posunG.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
		objScale.addChild( posunG );
		
		mat = new Material( zlta, eColor, zlta, sColor, 128 );
		mat.setLightingEnable( true );		
		ap = new Appearance();
		ap.setMaterial( mat );
		try 
		{
			url = new URL( "http://andrej-cicmansky.own.cz/Obrazky/textury/merkur.jpg" );
			loader = new TextureLoader( url , this );
			textura = loader.getTexture();		
			ap.setTexture( textura );
			ap.setTextureAttributes( texA );
		} 
		catch ( MalformedURLException e ) 
		{
			e.printStackTrace();
		}
		Sphere merkur = new Sphere( 0.01f, primflags, ap );
		merkur.setPickable( true );
		
		TransformGroup posunMerkur = new TransformGroup();
		posunMerkur.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );		
		Transform3D posun = new Transform3D();
		TransformGroup posunG1 = new TransformGroup();
		posunG1.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
		Vector3f posunV = new Vector3f( 0.2f, 0.0f, 0.0f );
		posun.setTranslation( posunV );
		posunG1.setTransform( posun );
		posunG1.addChild( merkur );
		posunMerkur.addChild( posunG1 );
		posunG.addChild( posunMerkur );
		
		mat = new Material( zlta, eColor, zlta, sColor, 128 );
		mat.setLightingEnable( true );		
		ap = new Appearance();
		ap.setMaterial( mat );
		try 
		{
			url = new URL( "http://andrej-cicmansky.own.cz/Obrazky/textury/venusa.jpg" );
			loader = new TextureLoader( url , this );
			textura = loader.getTexture();		
			ap.setTexture( textura );
			ap.setTextureAttributes( texA );
		} 
		catch ( MalformedURLException e ) 
		{
			e.printStackTrace();
		}
		Sphere venusa = new Sphere( 0.025f, primflags, ap);
		venusa.setPickable( true );
		
		TransformGroup posunVenusa = new TransformGroup();
		posunVenusa.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
		posun = new Transform3D();		
		TransformGroup posunG2 = new TransformGroup();
		posunG2.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
		posunV = new Vector3f( 0.28f, 0.0f, 0.0f );
		posun.setTranslation( posunV );
		posunG2.setTransform( posun );
		posunG2.addChild( venusa );
		posunVenusa.addChild( posunG2 );
		posunG.addChild( posunVenusa );
		
		
		mat = new Material( zlta, eColor, zlta, sColor, 128 );
		mat.setLightingEnable( true );		
		ap = new Appearance();
		ap.setMaterial( mat );
		try 
		{
			url = new URL( "http://andrej-cicmansky.own.cz/Obrazky/textury/zem.jpg" );
			loader = new TextureLoader( url , this );
			textura = loader.getTexture();		
			ap.setTexture( textura );
			ap.setTextureAttributes( texA );
		} 
		catch ( MalformedURLException e ) 
		{
			e.printStackTrace();
		}
		Sphere zem = new Sphere( 0.03f, primflags, ap );
		zem.setPickable( true );
		
		TransformGroup posunZem = new TransformGroup();
		posunZem.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
		posun = new Transform3D();	
		TransformGroup posunG3 = new TransformGroup();
		posunG3.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
		posunV = new Vector3f( 0.37f, 0.0f, 0.0f );
		posun.setTranslation( posunV );
		posunG3.setTransform( posun );
		posunG3.addChild( zem );
		posunZem.addChild( posunG3 );
		posunG.addChild( posunZem );
		
		
		mat = new Material( zlta, eColor, zlta, sColor, 128 );
		mat.setLightingEnable( true );		
		ap = new Appearance();
		ap.setMaterial( mat );
		try 
		{
			url = new URL( "http://andrej-cicmansky.own.cz/Obrazky/textury/mars.jpg" );
			loader = new TextureLoader( url , this );
			textura = loader.getTexture();		
			ap.setTexture( textura );
			ap.setTextureAttributes( texA );
		} 
		catch ( MalformedURLException e ) 
		{
			e.printStackTrace();
		}
		Sphere mars = new Sphere( 0.019f, primflags, ap );
		mars.setPickable( true );
		
		TransformGroup posunMars = new TransformGroup();
		posunMars.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
		posun = new Transform3D();		
		TransformGroup posunG4 = new TransformGroup();
		posunG4.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
		posunV = new Vector3f( 0.46f, 0.0f, 0.0f );
		posun.setTranslation( posunV );
		posunG4.setTransform( posun );
		posunG4.addChild( mars );
		posunMars.addChild( posunG4 );
		posunG.addChild( posunMars );
		
		
		mat = new Material( zlta, eColor, zlta, sColor, 128 );
		mat.setLightingEnable( true );		
		ap = new Appearance();
		ap.setMaterial( mat );
		try 
		{
			url = new URL( "http://andrej-cicmansky.own.cz/Obrazky/textury/jupiter.jpg" );
			loader = new TextureLoader( url , this );
			textura = loader.getTexture();		
			ap.setTexture( textura );
			ap.setTextureAttributes( texA );
		} 
		catch ( MalformedURLException e ) 
		{
			e.printStackTrace();
		}
		Sphere jupiter = new Sphere( 0.06f, primflags, ap );
		jupiter.setPickable( true );
		
		TransformGroup posunJupiter = new TransformGroup();
		posunJupiter.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
		posun = new Transform3D();		
		TransformGroup posunG5 = new TransformGroup();
		posunG5.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
		posunV = new Vector3f( 0.58f, 0.0f, 0.0f );
		posun.setTranslation( posunV );
		posunG5.setTransform( posun );
		posunG5.addChild( jupiter );
		posunJupiter.addChild( posunG5 );
		posunG.addChild( posunJupiter );
		
		
		mat = new Material( zlta, eColor, zlta, sColor, 128 );
		mat.setLightingEnable( true );		
		ap = new Appearance();
		ap.setMaterial( mat );
		try 
		{
			url = new URL( "http://andrej-cicmansky.own.cz/Obrazky/textury/saturn.jpg" );
			loader = new TextureLoader( url , this );
			textura = loader.getTexture();		
			ap.setTexture( textura );
			ap.setTextureAttributes( texA );
		} 
		catch ( MalformedURLException e ) 
		{
			e.printStackTrace();
		}
		Sphere saturn = new Sphere( 0.05f, primflags, ap );
		saturn.setPickable( true );

		TransformGroup posunSaturn = new TransformGroup();
		posunSaturn.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
		posun = new Transform3D();		
		TransformGroup posunG6 = new TransformGroup();
		posunG6.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
		posunV = new Vector3f( 0.72f, 0.0f, 0.0f );
		posun.setTranslation( posunV );
		posunG6.setTransform( posun );
		posunG6.addChild( saturn );
		posunSaturn.addChild( posunG6 );
		posunG.addChild( posunSaturn );
		
		
		mat = new Material( zlta, eColor, zlta, sColor, 128 );
		mat.setLightingEnable( true );		
		ap = new Appearance();
		ap.setMaterial( mat );
		try 
		{
			url = new URL( "http://andrej-cicmansky.own.cz/Obrazky/textury/uran.jpg" );
			loader = new TextureLoader( url , this );
			textura = loader.getTexture();		
			ap.setTexture( textura );
			ap.setTextureAttributes( texA );
		} 
		catch ( MalformedURLException e ) 
		{
			e.printStackTrace();
		}
		Sphere uran = new Sphere( 0.04f, primflags, ap );
		uran.setPickable( true );

		TransformGroup posunUran = new TransformGroup();
		posunUran.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
		posun = new Transform3D();		
		TransformGroup posunG7 = new TransformGroup();
		posunG7.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
		posunV = new Vector3f( 0.85f, 0.0f, 0.0f );
		posun.setTranslation( posunV );
		posunG7.setTransform( posun );
		posunG7.addChild( uran );
		posunUran.addChild( posunG7 );
		posunG.addChild( posunUran );
		
		
		mat = new Material( zlta, eColor, zlta, sColor, 128 );
		mat.setLightingEnable( true );		
		ap = new Appearance();
		ap.setMaterial( mat );
		try 
		{
			url = new URL( "http://andrej-cicmansky.own.cz/Obrazky/textury/neptun.jpg" );
			loader = new TextureLoader( url , this );
			textura = loader.getTexture();		
			ap.setTexture( textura );
			ap.setTextureAttributes( texA );
		} 
		catch ( MalformedURLException e ) 
		{
			e.printStackTrace();
		}
		Sphere neptun = new Sphere( 0.039f, primflags, ap );
		neptun.setPickable( true );

		TransformGroup posunNeptun = new TransformGroup();
		posunNeptun.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
		posun = new Transform3D();		
		TransformGroup posunG8 = new TransformGroup();
		posunG8.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
		posunV = new Vector3f( 0.95f, 0.0f, 0.0f );
		posun.setTranslation( posunV );
		posunG8.setTransform( posun );
		posunG8.addChild( neptun );
		posunNeptun.addChild( posunG8 );
		posunG.addChild( posunNeptun );		
			
		//svetlo
		TransformGroup rotTrans = new TransformGroup();
		rotTrans.setPickable( false );
		rotTrans.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );		
		objScale.addChild( rotTrans );		
		
		Transform3D trans = new Transform3D();
		Vector3d vec = new Vector3d( 0.0, 0.0, 2.0 );
		trans.set( vec );
		TransformGroup transG = new TransformGroup( trans );
		rotTrans.addChild( transG );		
		PointLight light = new PointLight( new Color3f( Color.white ), pointF, new Point3f( 0.0f, 0.0f, 1.00f ) );
		light.setInfluencingBounds( bounds );
		transG.addChild( light );		
		
		
		//pohyb planet		
		Transform3D yAxis = new Transform3D();
	    Alpha alpha1 = new Alpha( -1, Alpha.INCREASING_ENABLE, 0, 0, 964, 0, 0, 0, 0, 0 );
	    RotationInterpolator rotator1 = new RotationInterpolator( alpha1, posunMerkur, yAxis, 0.0f, (float) Math.PI * 2.0f );
	    rotator1.setSchedulingBounds( bounds );
	    posunG1.addChild( rotator1 );
	    
	    Alpha alpha2 = new Alpha( -1, Alpha.INCREASING_ENABLE, 0, 0, 2460, 0, 0, 0, 0, 0 );
	    RotationInterpolator rotator2 = new RotationInterpolator( alpha2, posunVenusa, yAxis, 0.0f, (float) Math.PI * 2.0f );
	    rotator2.setSchedulingBounds( bounds );
	    posunG2.addChild( rotator2 );
	    
	    Alpha alpha3 = new Alpha( -1, Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0 );
	    RotationInterpolator rotator3 = new RotationInterpolator( alpha3, posunZem, yAxis, 0.0f, (float) Math.PI * 2.0f );
	    rotator3.setSchedulingBounds( bounds );
	    posunG3.addChild( rotator3 );
	    
	    Alpha alpha4 = new Alpha( -1, Alpha.INCREASING_ENABLE, 0, 0, 7520, 0, 0, 0, 0, 0 );
	    RotationInterpolator rotator4 = new RotationInterpolator( alpha4, posunMars, yAxis, 0.0f, (float) Math.PI * 2.0f );
	    rotator4.setSchedulingBounds( bounds );
	    posunG4.addChild( rotator4 );
	    
	    Alpha alpha5 = new Alpha( -1, Alpha.INCREASING_ENABLE, 0, 0, 47440, 0, 0, 0, 0, 0 );
	    RotationInterpolator rotator5 = new RotationInterpolator( alpha5, posunJupiter, yAxis, 0.0f, (float) Math.PI * 2.0f );
	    rotator5.setSchedulingBounds( bounds );
	    posunG5.addChild( rotator5 );
	    
	    Alpha alpha6 = new Alpha( -1, Alpha.INCREASING_ENABLE, 0, 0, 117840, 0, 0, 0, 0, 0 );
	    RotationInterpolator rotator6 = new RotationInterpolator( alpha6, posunSaturn, yAxis, 0.0f, (float) Math.PI * 2.0f );
	    rotator6.setSchedulingBounds( bounds );
	    posunG6.addChild( rotator6 );
	    
	    Alpha alpha7 = new Alpha( -1, Alpha.INCREASING_ENABLE, 0, 0, 336040, 0, 0, 0, 0, 0 );
	    RotationInterpolator rotator7 = new RotationInterpolator( alpha7, posunUran, yAxis, 0.0f, (float) Math.PI * 2.0f );
	    rotator7.setSchedulingBounds( bounds );
	    posunG7.addChild( rotator7 );
	    
	    Alpha alpha8 = new Alpha( -1, Alpha.INCREASING_ENABLE, 0, 0, 659160, 0, 0, 0, 0, 0 );
	    RotationInterpolator rotator8 = new RotationInterpolator( alpha8, posunNeptun, yAxis, 0.0f, (float) Math.PI * 2.0f );
	    rotator8.setSchedulingBounds( bounds );
	    posunG8.addChild( rotator8 );
	    
	    //rotacia
	    MouseRotate myRotate = new MouseRotate();
	    myRotate.setTransformGroup( objScale );
	    myRotate.setSchedulingBounds( bounds );
	    objScale.addChild( myRotate );		
		
		branch.compile();
		
		return branch;
	}		
	
	public void init()
	{
		setSize( 600, 600 );
		setLayout( new BorderLayout() );
		
		GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
		Canvas3D canvas = new Canvas3D( config );	
		universe = new SimpleUniverse( canvas );		
		add( "Center", canvas );
		
		BranchGroup scene = createSceneGraph( universe );		
		universe.getViewingPlatform().setNominalViewingTransform();		
		universe.addBranchGraph( scene );		
	}
}